﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GriffinWebService.BE;
using System.Data;
using System.Text;
using GriffinWebService.Entity;

namespace GriffinWebService.DALC
{
    public class bookmarkDALC
    {
        #region Fields
        private SqlHelper _sqlHelper = new SqlHelper();
        #endregion

        #region Methods
        public int AddBookmark(Bookmark bookmark)
        {
            int ret = 0;
            string statement = string.Empty;

            try
            {
                if (bookmark == null)
                {
                    throw new ArgumentNullException("bookmark");
                }

                statement = "dbo.sp_bookmark_add";
                if (string.IsNullOrEmpty(bookmark.UserID.ToString()))
                {
                    _sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    _sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.NVarChar, bookmark.UserID.ToString());
                }
                if (string.IsNullOrEmpty(bookmark.Bookmarkname))
                {
                    _sqlHelper.AddParameterToSQLCommand("@bookmarkname", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    _sqlHelper.AddParameterToSQLCommand("@bookmarkname", SqlDbType.NVarChar, bookmark.Bookmarkname);
                }
                if (string.IsNullOrEmpty(bookmark.Bookmarkurl))
                {
                    _sqlHelper.AddParameterToSQLCommand("@bookmarkurl", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    _sqlHelper.AddParameterToSQLCommand("@bookmarkurl", SqlDbType.NVarChar, bookmark.Bookmarkurl);
                }
                
                int result = _sqlHelper.GetExecuteScalarByCommand(statement);
                if (result == 0)
                {
                    ret = 1;
                }

            }
            finally
            {
                _sqlHelper.Dispose();
            }

            return ret;
        }

        public List<Bookmark> GetBookmarks(User user)
        {
            string strWhere = "UserID = " + user.UserID;

            return GetModelList(strWhere);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserID, Bookmarkname, Bookmarkurl");
            strSql.Append(" FROM Bookmark ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return _sqlHelper.GetDataset(strSql.ToString());
        }

        public List<Bookmark> GetModelList(string strWhere)
        {
            DataSet ds = GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// dataset to modellist
        /// </summary>
        public List<Bookmark> DataTableToList(DataTable dt)
        {
            List<Bookmark> modelList = new List<Bookmark>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Bookmark model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Bookmark();
                    if (dt.Rows[n]["UserID"].ToString() != "")
                    {
                        model.UserID = int.Parse(dt.Rows[n]["UserID"].ToString());
                    }
                    model.Bookmarkname = dt.Rows[n]["Bookmarkname"].ToString();
                    model.Bookmarkurl = dt.Rows[n]["Bookmarkurl"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 2011-04-11 by edisun
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="bookmarkName"></param>
        /// <param name="bookmarkUrl"></param>
        /// <returns></returns>
        public int editBookmarks(int userid, string bookmarkName, string bookmarkUrl)
        {
            string statement = string.Empty;
            int result = 0;

            #region
            try
            {

                statement = "dbo.spu_bookmark_update";
                _sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.Int, userid);
                _sqlHelper.AddParameterToSQLCommand("@bookmarkname", SqlDbType.NVarChar, bookmarkName);
                _sqlHelper.AddParameterToSQLCommand("@bookmarkurl", SqlDbType.NVarChar, bookmarkUrl);

                int res = _sqlHelper.GetExecuteScalarByCommand(statement);
                result = res ;

            }
            finally
            {
                _sqlHelper.Dispose();
            }
            #endregion


            return result;
            
        }

        public int deleteBookmarks(int userid, string bookmarkName)
        {
            string statement = string.Empty;
            int result = 0;

            #region
            try
            {

                statement = "dbo.spu_bookmark_delete";
                _sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.Int, userid);
                _sqlHelper.AddParameterToSQLCommand("@bookmarkname", SqlDbType.NVarChar, bookmarkName);

                int res = _sqlHelper.GetExecuteScalarByCommand(statement);
                result = res ;

            }
            finally
            {
                _sqlHelper.Dispose();
            }
            #endregion


            return result;
        }

        #endregion
    }
}
